package com.yellowpage.ca.dao;

import java.util.List;
import java.util.Map;

import com.yellowpage.ca.data.User;
import com.yellowpage.white.dao.BaseDaoHibernateImplExtend;
import com.yellowpage.white.dao.QueryAssistantSQL;

public class UserDaoImpl extends BaseDaoHibernateImplExtend<User, Long> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	public User getUserByName(String name) {
		String queryString = "from User u where u.name = '" + name + "'";
		List userList = getHibernateTemplate().find(queryString);
		if (userList.size() > 0) {
			return (User)userList.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getFunctionsByUser(Long userId) {
		String sql = "select distinct e.* " +
				"       from CA_ROLE_FUNCTION a,CA_ROLE b,CA_USER_ROLE c,CA_USER d,CA_FUNCTION e "
				+ "where a.role_id=b.role_id and b.role_id=c.role_id and c.user_id=d.user_id and a.function_id=e.function_id and d.user_id="
				+ userId;
		QueryAssistantSQL sqlAssistant = newQueryAssistantSQL(sql);

		List<Map<String, Object>> resultList = sqlAssistant.query(
				getConnection(), null);

		return resultList;
	}

}